package com.lucifer.sort;

/**
 * 计数排序法
 *
 * @author yuxiao05@meituan.com
 * @create 2016-07-05 上午11:04
 */

public class CountingSort {
  public static void main(String[] args){
    int[] a = new int[]{2,1,4,5,3,2,6};
    int[] count = new int[10];

    for (int i = -1; ++i < a.length; count[a[i]]++);
    for (int i = 10, k = a.length; k > 0; ) {
      while (count[--i] == 0);
      int value = i;
      int s = count[i];

      do {
        a[--k] = value;
      } while (--s > 0);
    }

    for (int j : a)
      System.out.println(j);

  }
}
